package com.spring.data.jpa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.data.jpa.domain.User;
import com.spring.data.jpa.service.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value = "/user")
	public String home() {

		return "WEB-INF/views/user.jsp";
	}
	
	@RequestMapping(value = "/searchUsers", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> search(@RequestParam int page,@RequestParam int start,@RequestParam int limit,User user){
		
		Map<String, Object> returnMap = userService.searchByCondition(page,start,limit,user);
		
		return returnMap;
	}
	@RequestMapping(value = "/editUser")
	public @ResponseBody Map<String, Object> eidtUser(User user){
		
		userService.saveUser(user);
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("success", true);
		returnMap.put("msg", "操作成功！");
		
		return returnMap;
	}
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> deleteUser(String ids){
		
		System.out.println(ids);
		String ids1 = ids.substring(1, ids.length()-1);
		String[] userids = ids1.split(",");
		for(String id:userids){
			userService.deleteUserById(Integer.parseInt(id));
		}
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("success", true);
		returnMap.put("msg", "操作成功！");
		
		return returnMap;
	}
	
}
